//index.js
//获取应用实例
const app = getApp()
function transDate(mescStr) {
  var n = mescStr;
  var date = new Date(n);
  var Y = date.getFullYear() + '-';
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  return (Y + M + D)
}

Page({
  data:
  {
    progressList: []
  },
  /**
  * 跳转进度详情页面
  */

  progressTap(e) {
    var $code = e.currentTarget.dataset.code;
    var status = e.currentTarget.dataset.status;
    var id = e.currentTarget.dataset.id;
    
    if ($code == 'demand_progress_product') {
      if (status == 0) {
        wx.navigateTo({
          url: '../pickProduct/pickProduct?id=' + id,
        })
      }else{
        wx.navigateTo({
          url: '../myproduct/myproduct?id=' + id,
        })
      }
    }else{
      wx.navigateTo({
        url: '../myproduct/myproduct?id=' + id,
      })
    }
    
  },
  onLoad: function () {
    var userInfo = wx.getStorageSync('userInfo');
    if (userInfo == "") {
      wx.reLaunch({
        url: '../auth/auth',
      })
      return
    }
    var that = this;
    wx.request({
      url: app.globalData.BaseURL + '/customerProgress',
      method: 'GET',
    
      header: {
        'Authorization': userInfo.token
      },
      //请求后台数据成功  
      success: function (res) {
        wx.showToast({
          title: '加载中',
          icon: 'loading',
          duration: 600
        });
        if (res.statusCode == 200) {
          if (res.data.code == 0) {
            var tmp = res.data.data
            tmp.forEach(function (t) {
              t.deal_time = transDate(t.deal_time);
              if (t.status == 0) {
                t.background = "grey"
              } else if (t.status == 2) {
                t.background = "red"
              } else {
                t.background = "lightgreen"
              }
            });
            that.setData({
              progressList: tmp,
            })
          } else {
            wx.showToast({
              title: res.data.message,
            })
          }
        } else {
          wx.reLaunch({
            url: '../auth/auth',
          })
        }
      }
    })

   },
    onPullDownRefresh(){
      var userInfo = wx.getStorageSync('userInfo');
      if (userInfo == "") {
        wx.reLaunch({
          url: '../auth/auth',
        })
        return
      }
      var that = this;
      wx.showToast({
        title: '刷新中',
        icon: 'loading',
        duration: 600
      });
      wx.request({
        url: app.globalData.BaseURL + '/customerProgress',
        method: 'GET',
        header: {
          'Authorization': userInfo.token
        },
        //请求后台数据成功  
        success: function (res) {
          if (res.statusCode == 200) {
            if (res.data.code == 0) {
              var tmp = res.data.data
              tmp.forEach(function (t) {
                t.deal_time = transDate(t.deal_time);
                if (t.status == 0) {
                  t.background = "grey"
                } else if (t.status == 2) {
                  t.background = "red"
                } else {
                  t.background = "lightgreen"
                }
              });
              that.setData({
                progressList: tmp,
              })
            } else {
              wx.showToast({
                title: res.data.message,
              })
            }
          } else {
            wx.reLaunch({
              url: '../auth/auth',
            })
          }
        }
      })


    }
  },

)

